﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BestLifePOS_Core.Data.Core;
using BestLifePOS_Core.DomainObjects;
using System.Data.SqlClient;
using System.Data;
using BestLifePOS_Core.Exceptions.DataBase;

namespace BestLifePOS_Core.Data.Commands
{
    class BestLifeUserByUsernameDataAction:BestLifeDataAction<List<User>>
    {
        private string GvUserName;
        public BestLifeUserByUsernameDataAction(string PUserName)
        {
            GvUserName = PUserName;
        }
        public override List<User> Body(SqlConnection PConn)
        {
            try
            {
                List<User> LvList = new List<User>();


                SqlCommand LvCommand = new SqlCommand();
                LvCommand.Connection = PConn;
                LvCommand.CommandType = CommandType.StoredProcedure;
                LvCommand.CommandText = "dbo.GetUserByUsername";
                LvCommand.Parameters.Add(new SqlParameter("@Use_Name", GvUserName));//when passing parameters to the SP

                SqlDataReader LvReader = LvCommand.ExecuteReader();
                while (LvReader.Read())
                {
                    User LvUser = new User();
                    LvUser.User_Id=LvReader.GetValue(0).ToString();
                    LvUser.Use_Type=LvReader.GetValue(1).ToString();
                    LvUser.Use_Name=LvReader.GetValue(2).ToString();
                    LvUser.Use_Id=LvReader.GetValue(3).ToString();
                    //LvUser.Last_Modify =Convert.ToDateTime(LvReader.GetValue(4).ToString());
                    LvUser.Pas_Word=LvReader.GetValue(5).ToString();
                    LvUser.User_Access=LvReader.GetValue(6).ToString();
                    LvUser.User_Modify=LvReader.GetValue(7).ToString();
                    //LvUser.Date_Create =Convert.ToDateTime(LvReader.GetValue(8).ToString());
                    LvList.Add(LvUser);
                }
                return LvList;
            }
            catch (BestlifeDatabaseException e)
            {
                throw new BestlifeDatabaseException(e);
            }
        }
    }
}

